﻿using MySqlSugar;
using System;
using System.Collections.Generic;

namespace Repositories
{
    /// <summary>
    /// SqlSugar
    /// </summary>
    public class SugarDao
    {
        //禁止实例化
        private SugarDao()
        {

        }
        /// <summary>
        /// 全局配置类
        /// </summary>
        public class SugarConfigs
        {
            //key类名 value表名
            public static List<KeyValue> MpList = new List<KeyValue>(){
            new KeyValue(){ Key="ArticleModel", Value="Article"},
            new KeyValue(){ Key="ArticletypeModel", Value="Articletype"},
            new KeyValue(){ Key="ConfigModel", Value="Config"},
            new KeyValue(){ Key="LogModel", Value="Log"},
            new KeyValue(){ Key="MenuModel", Value="Menu"},
            new KeyValue(){ Key="ProductModel", Value="Product"},
            new KeyValue(){ Key="ProducttypeModel", Value="Producttype"},
            new KeyValue(){ Key="ReplyModel", Value="Reply"},
            new KeyValue(){ Key="RolemenuModel", Value="Rolemenu"},
            new KeyValue(){ Key="RoleModel", Value="Role"},
            new KeyValue(){ Key="UsersModel", Value="Users"},
            new KeyValue(){ Key="VarticleModel", Value="Varticle"},
            new KeyValue(){ Key="VmenuModel", Value="Vmenu"},
            new KeyValue(){ Key="VproductModel", Value="Vproduct"},
            new KeyValue(){ Key="VusersModel", Value="Vusers"},
            new KeyValue(){ Key="VlogModel", Value="Vlog"},
            new KeyValue(){ Key="VreplyModel", Value="Vreply"}
            };
        }

        //public static string ConnectionString
        //{
        //    get
        //    {
        //        string reval = "server=localhost;Database=SqlSugarTest;Uid=root;Pwd=123456";
        //        return reval;
        //    }
        //}

        public static SqlSugarClient GetInstance()
        {
            var db = new SqlSugarClient(Utility.SiteConfig.SiteConfigStr);
            db.SetMappingTables(SugarConfigs.MpList);//设置关联表 (引用地址赋值，每次赋值都只是存储一个内存地址)
            //var db = new SqlSugarClient(sqlstr);
            db.IsEnableLogEvent = true;//启用日志事件
            db.LogEventStarting = (sql, par) => { Console.WriteLine(sql + " " + par + "\r\n"); };
            return db;
        }
    }
}
